Input Loops¶
⚙️ Types¶
1 + 1
2
'1' + '1'
'11'
type(1)
int
type(True)
bool
type('1')
str
When you put quotes around text, that text becomes a string (str
).
Strings are sequences of graphical symbols.
2
is the numeric integer "two".
'2'
is the graphical symbol "2".
🎨 Basic Operators¶
10 + 7
17
When used with int
s, +
adds them together.
'fire' + 'place'
'fireplace'
When used with str
s, +
concatenates them together.
7 - 3
4
-
does subtraction.
'nickname' - 'name'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[10], line 1 ----> 1 'nickname' - 'name' TypeError: unsupported operand type(s) for -: 'str' and 'str'
Python doesn't let you subtract strings (only "add" them).
"water bottle" - "banana"
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[11], line 1 ----> 1 "water bottle" - "banana" TypeError: unsupported operand type(s) for -: 'str' and 'str'
🎨 Comparisons¶
8 == 9
False
9 == 9
True
"cat" == "dog"
False
"cat" == "cat"
True
You use ==
(two equals signs) to see if two things are equal.
You use =
(one equals sign) to give a variable a value.
number = 9
if number == 8:
print('The number is 8')
else:
print('The number is not 8')
The number is not 8
8 > 7
True
3 < 8
True
"aardvark" < "zebra"
True
"cassowary" > "banana boat"
True
'mango' < 'muffin'
True
'cat' < 'catastrophe'
True
'Zebra' < 'aardvark'
True
Using >
or <
on strings tells you which string comes before the other alphabetically.
The "aphabetical order" of strings is defined by the ASCII table:
...0123456789...ABCDEFG...abcdefg...
"banana" > "🫢"
False
🖌 Input Loops¶
road_trip.py
¶
break
means break out of the current loop.
It doesn't matter whether the condition is True
or False
, break
will stop the loop.
The next line of code to run is whatever follows the while
loop.
NOTE
Yes:
response == "yes" or response == "Yes"
No:
response == "yes" or "Yes"
int
¶
"123" > "23"
False
123 > 23
True
bigger.py
¶
input
always returns a string.
You can use int()
to turn a string into an integer.
Key Ideas¶
- types
int
vsstr
+
,-
+
also works on strings
==
,<
,>
- work with both integers and strings
- Input loops
break
orreturn
to get out of a loop